package com.sora.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sora.eduservice.client.VodClient;
import com.sora.eduservice.entity.EduVideo;
import com.sora.eduservice.mapper.EduVideoMapper;
import com.sora.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author sora
 * @since 2022-12-28
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        List<String> list = eduVideos.stream().map(e -> {
            String sourceId = e.getVideoSourceId();
            return sourceId;
        }).collect(Collectors.toList());
        if (list.size() > 0) {
            vodClient.removeBatch(list);
        }
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        baseMapper.delete(queryWrapper2);
    }
}
